package com.adou.shiro.filter;

import com.adou.common.util.IConfig;
import com.adou.shiro.token.TokenManager;
import com.adou.shiro.util.ShiroFilterUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.Map;

public class SimpleFilter extends AccessControlFilter {

    private static final Logger logger = LoggerFactory.getLogger(SimpleFilter.class);

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        String systype = IConfig.get("systype");
        if (systype == null || systype.equals("")) {
            Map<String, String> resultMap = new HashMap<String, String>();
            logger.error("子系统类型参数丢失！");
            resultMap.put("result", "751");
            resultMap.put("msg", "子系统类型参数丢失");//当前用户参数异常！
            ShiroFilterUtils.out(servletResponse, resultMap);
            return Boolean.FALSE;
        } else {
            TokenManager.setVal2Session("systype", systype);
        }
        return Boolean.TRUE;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        return Boolean.FALSE;
    }

}
